package com.ifcar.app.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.ifcar.app.activity.MyApplication;
import com.ifcar.app.db.NewsSQLiteOpenHelper;

public class CollectUtil {
	
	/**文章*/
	public static final int COLLECT_ARTICLE = 1;	
	/**图片*/
	public static final int COLLECT_PICTURES = 2;
	/**跟贴*/
	public static final int COLLECT_COMMENT = 3;
	/**图片的跟贴*/
	public static final int COLLECT_PIC_COMMENT = 4;
	
	private SQLiteDatabase db;
	
	public CollectUtil(Context context){
		MyApplication app  = (MyApplication)context.getApplicationContext();
		db = app.getSQLiteDatabase();
	}
	
	/**
	 * 收藏
	 * @param path 收藏 path
	 * @param title 收藏 标题
	 * @param id 收藏 id
	 * @param type 收藏类型 static of CollectUtil[COLLECT_ARTICLE, COLLECT_PICTURES, COLLECT_COMMENT, COLLECT_PIC_COMMENT]
	 * @return int -1 为失败 ， 其他为成功
	 */
	public Long doCollectObject(String path, String title, String id, int type){
		Long l = new Long(0);

		String date = StringUtil.getCurrnetDate("yyyy-MM-dd");
		
		ContentValues values = new ContentValues();
		
		values.put(NewsSQLiteOpenHelper.COLLECT_PATH, path);
		values.put(NewsSQLiteOpenHelper.COLLECT_TITLE, title);
		values.put(NewsSQLiteOpenHelper.COLLECT_OBJID, id);
		values.put(NewsSQLiteOpenHelper.COLLECT_TYPE, type);
		
		values.put(NewsSQLiteOpenHelper.COLLECT_CREATE_AT, date);
		
		l = db.insert(NewsSQLiteOpenHelper.COLLECT_TABLE, null, values);						

		return l;
	}

	/**
	 * 根据path， title， id， type 查询已经收藏的数据
	 * @param path 收藏连接
	 * @param id 收藏ID
	 * @param type 收藏类型 static of CollectUtil[COLLECT_ARTICLE, COLLECT_PICTURES, COLLECT_COMMENT]
	 * @return -1 为没有收藏， 其它值为收藏
	 */
	public int getAlreadyCollect(String path, String id, int type) {
		
		
		StringBuilder condition = new StringBuilder();
		
		if(type==COLLECT_COMMENT || type == COLLECT_PIC_COMMENT){
			condition.append(NewsSQLiteOpenHelper.COLLECT_PATH).append("='").append(path).append("'");
			condition.append(" and ").append(NewsSQLiteOpenHelper.COLLECT_TYPE).append("='").append(type).append("'");
		}else{
			condition.append(NewsSQLiteOpenHelper.COLLECT_OBJID).append("='").append(id).append("'");
			condition.append(" and ").append(NewsSQLiteOpenHelper.COLLECT_TYPE).append("='").append(type).append("'");			
		}
		
		Cursor cur = db.query(NewsSQLiteOpenHelper.COLLECT_TABLE, new String[]{NewsSQLiteOpenHelper.COLLECT_AUTOID},
				condition.toString() , null, null, null, null);

		int autoId = -1;
		if(cur.moveToFirst()){
			int indexid = cur.getColumnIndex(NewsSQLiteOpenHelper.COLLECT_AUTOID);
			autoId = cur.getInt(indexid);
		}
		cur.close();

		return autoId;
	}

	/**
	 * 取消收藏
	 * @param autoId
	 * @param type
	 */
	public int deleteAlreadyCollect(int autoId, int type) {
		
		StringBuilder condition = new StringBuilder();
		condition.append(NewsSQLiteOpenHelper.COLLECT_AUTOID).append("=?");
		condition.append(" and ").append(NewsSQLiteOpenHelper.COLLECT_TYPE).append("=?");
		
		int flag = db.delete(NewsSQLiteOpenHelper.COLLECT_TABLE, condition.toString(), new String[]{""+autoId, ""+type});

		return flag;
		
	}
	
	/**
	 * 根据不同的类型查找收藏数据
	 * @param type
	 * @return Cursor
	 */
	public List<Map<String, Object>> getCollectObjects(int type){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		StringBuilder condition = new StringBuilder();
		String results[] = null;
		
		if(type==CollectUtil.COLLECT_COMMENT){
			results = new String[]{type+"", CollectUtil.COLLECT_PIC_COMMENT+""};
			condition.append(NewsSQLiteOpenHelper.COLLECT_TYPE).append(" =? or ").append(NewsSQLiteOpenHelper.COLLECT_TYPE).append(" =?");
		}else{
			results = new String[]{type+""};
			condition.append(NewsSQLiteOpenHelper.COLLECT_TYPE).append("=?");
		}
		
		Cursor cur = db.query(NewsSQLiteOpenHelper.COLLECT_TABLE, new String[]{NewsSQLiteOpenHelper.COLLECT_AUTOID, NewsSQLiteOpenHelper.COLLECT_TITLE, 
				NewsSQLiteOpenHelper.COLLECT_PATH, NewsSQLiteOpenHelper.COLLECT_OBJID, NewsSQLiteOpenHelper.COLLECT_TYPE},
				condition.toString() , results, null, null, NewsSQLiteOpenHelper.COLLECT_CREATE_AT + " desc");
		
		if(cur.moveToFirst()){
			while(!cur.isAfterLast()){
				Map<String, Object> map = new HashMap<String,Object>();
				
				int autoIndex = cur.getColumnIndex(NewsSQLiteOpenHelper.COLLECT_AUTOID);
				map.put(NewsSQLiteOpenHelper.COLLECT_AUTOID, cur.getString(autoIndex));
				
				int pathIndex = cur.getColumnIndex(NewsSQLiteOpenHelper.COLLECT_PATH);
				Object o = cur.getString(pathIndex);
				String filename = null==o?"":o.toString();
				if(COLLECT_PICTURES==type){
					String fname = filename.substring(filename.lastIndexOf("/")+1, filename.length());					
					String imgName = PicUtil.getTempJPEGFile(fname);//Bitmap b = BitmapFactory.decodeFile(imageName);					
					map.put(NewsSQLiteOpenHelper.COLLECT_IMAGE_LOCAL, imgName);
				}
				map.put(NewsSQLiteOpenHelper.COLLECT_PATH, filename);
				
				int titleIndex = cur.getColumnIndex(NewsSQLiteOpenHelper.COLLECT_TITLE);
				map.put(NewsSQLiteOpenHelper.COLLECT_TITLE, cur.getString(titleIndex));
				
				int idIndex = cur.getColumnIndex(NewsSQLiteOpenHelper.COLLECT_OBJID);
				map.put(NewsSQLiteOpenHelper.COLLECT_OBJID, cur.getString(idIndex));
				
				int typeIndex = cur.getColumnIndex(NewsSQLiteOpenHelper.COLLECT_TYPE);
				map.put(NewsSQLiteOpenHelper.COLLECT_TYPE, cur.getString(typeIndex));
				
				list.add(map);
				cur.moveToNext();
			}
		}
		cur.close();
	
		
		return list;
	}
	
	/**
	 * 删除收藏
	 * @param path
	 * @param title
	 * @param id
	 * @param type
	 */
	public int deleteCollectById(String id) {
	
		StringBuilder condition = new StringBuilder();
		condition.append(NewsSQLiteOpenHelper.COLLECT_AUTOID).append("=?");
		
		int flag = db.delete(NewsSQLiteOpenHelper.COLLECT_TABLE, condition.toString(), new String[]{id});

		return flag;
		
	}
}


